package com.sinosoft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.report.ReportComments;
import com.sinosoft.report.ReportManager;
import com.sinosoft.report.ReportView;
import com.sinosoft.util.CheckEmpty;
import com.sinosoft.util.SystemConfigUtil;
@SuppressWarnings("unchecked")
@Controller
public class RoderReportController extends BaseFormController {

	private ReportManager reportMan;

	@Autowired
	public void setReportManager(ReportManager reportMan) {
		this.reportMan = reportMan;
	}

	
	@RequestMapping("report/orderreport.html")
	protected ModelAndView countUserList(ReportView vo,Model model,HttpServletRequest request,
			HttpServletResponse response) {
		String group = ReportComments.countStr(vo.getGroup(), ",");
		String order = ReportComments.filterOrder(vo.getGroup(), vo.getOrder(), ",");
		vo.setStrgroup(group);
		vo.setStrorder(order);
		List list = reportMan.searchOrder(vo);
		model.addAttribute("reportview", vo);
		if(null!=list&&list.size()>0){
			model.addAttribute("listresult", true);
		}else{
			model.addAttribute("listresult", false);
		}
		// 卫星
		String[] satellite = SystemConfigUtil.getSystemValue("satellite", this
				.getServletContext());
		// 接收站
		String[] receivingstation = SystemConfigUtil.getSystemValue("receivingstation", this
				.getServletContext());
		//卫星传感器
		if(!CheckEmpty.isEmpty(vo.getSatellte())){
			String[] senor = SystemConfigUtil.getSystemValue(vo.getSatellte(),
					getServletContext());
			model.addAttribute("senor", senor);
		}
		model.addAttribute("receivingstation", receivingstation);
		model.addAttribute("satellite", satellite);
	
		return new ModelAndView("/report/orderreport", "orderlist",list);
	}

	/**
	 * 订单图形统计
	 * @param starttime
	 * @param endtime
	 * @param stry
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("report/orderpicreport.html")
	protected ModelAndView countUserLists(String starttime, String endtime,
			String stry, HttpServletRequest request,
			HttpServletResponse response) {
		String group = stry;
		if (CheckEmpty.isEmpty(stry) || stry.equals("null") || stry.equals("")) {
			group = "orderstate";
		}
		List<ReportView> list = reportMan.searchOrder(group, starttime,
				endtime);
		Map<String, Integer> map = new HashMap<String, Integer>();
		int count=0;
		for (int i = 0; i < list.size(); i++) {
			ReportView vo = list.get(i);
			count+=vo.getNum();
			map.put(vo.getValue(), vo.getNum());
		}

		JSONObject jsonobj = new JSONObject();
		jsonobj.put("ingredients", map);
		jsonobj.put("count", count);
		try {
			response.getWriter().write(jsonobj.toString());
		} catch (Exception e) {

		}
		return null;
	}

}
